<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery/jquery.min.js"></script>

</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            用户信息修改
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form" id="form">
                <div class="form-group">
                    <label for="inputEmail1" class="col-sm-2 control-label">用户名称：</label>
                    <div class="col-sm-4">
                        <input type="text" name="username" class="form-control username" id="username" value="李思思">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail2" class="col-sm-2 control-label">昵称：</label>
                    <div class="col-sm-4">
                        <input type="text" name="nickname" class="form-control nickname" id="nickname" value="哈哈思">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">邮箱：</label>
                    <div class="col-sm-4">
                        <input type="email" name="email" class="form-control email" id="email" value="520@163.com">
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">用户图标：</label>
                    <div class="col-sm-10">
                        <img src="images/2.jpg" alt="" class="user_pic">
                        <input style="display: block" name="userPic" type="file" onchange="changeImg()"
                            id="exampleInputFile">
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputEmail4" class="col-sm-2 control-label">密码：</label>
                    <div class="col-sm-4">
                        <input type="password" name="password" class="form-control password" id="password"
                            value="12345678">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-success btn-edit">修改</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script src="./js/http.js"></script>
    <script>
        $(function () {
            // 获取个人信息
            get_user();

            // 修改
            $('#form').submit(function (e) {
                //阻止浏览器的默认提交事件
                e.preventDefault();
                // 获取页面信息
                let param = {
                    username: $('#username').val(),
                    nickname: $('#nickname').val(),
                    email: $('#email').val(),
                    userPic: document.querySelector('#exampleInputFile').files[0],
                    password: $('#password').val(),
                }
                let fromdata = new FormData();
                fromdata.append('username', $('#username').val());
                fromdata.append('nickname', $('#nickname').val());
                fromdata.append('email', $('#email').val());
                fromdata.append('userPic', document.querySelector('#exampleInputFile').files[0]);
                fromdata.append('password', $('#password').val());

                myAjax('post', '/admin/user/edit', fromdata, function (resData) {
                    if (resData.code !== 200) {
                        alert(resData.msg);
                    }
                    window.parent.location = './index.html';
                })
            });

        });

        // 获取个人信息，填入页面
        function get_user() {
            myAjax('get', '/admin/user/detail', null, function (resData) {
                // console.log(resData);
                $('#username').val(resData.data.username);
                $('#nickname').val(resData.data.nickname);
                $('#email').val(resData.data.email);

                // let file = document.querySelector('#inputCover').files[0];
                // let path = URL.createObjectURL(file);
                // 用户图标
                $('.user_pic').prop('src', resData.data.userPic);
                $('#password').val(resData.data.password);
            })

        }
        // 用户图标
        function changeImg() {
            console.log(1);
            // 1.0 获取文件对象
            let file = document.querySelector('#exampleInputFile').files[0];

            // 2.0 创建本地路径: blob:null/xxxxxxx
            let url = URL.createObjectURL(file);

            // 3.0 将图片本地路径赋值给img对象的src完成本地显示
            $('.user_pic').prop('src', url);
        }
    </script>
</body>

</html>